package com.oa.modules.approvalsec1.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.modules.approvalsec1.entity.InstitutionPersonEntity;
import com.oa.modules.approvalsec1.entity.InstitutionsEntity;
import com.oa.modules.approvalsec1.service.InstitutionPersonService;
import com.oa.modules.approvalsec1.service.InstitutionsService;
import com.oa.modules.approvalsec1.service.MattersService;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;


/**
 * 机构
 *
 * @author Hankaicheng
 * @email 785297226@qq.com
 * @date 2022-05-05 15:23:15
 */
@RestController
@RequestMapping("institutions")
@Api(tags = "机构相关接口")
public class InstitutionsController extends AbstractController {
    @Autowired
    private InstitutionsService institutionsService;

    @Autowired
    private MattersService mattersService;

    @Autowired
    private InstitutionPersonService institutionPersonService;


    @ApiOperation("列表")
    @GetMapping("/webList")
    public R webList(@RequestParam Map<String, Object> params) {
        PageUtils page = institutionsService.queryPage(params);
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "小程序列表")
    public R list(@RequestParam Map<String, Object> params) {
        List<InstitutionsEntity> list = institutionsService.list(new QueryWrapper<InstitutionsEntity>().eq("joint", params.get("jointId")));
        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") Integer id) {
        InstitutionsEntity institutions = institutionsService.getById(id);
        List<String> items = mattersService.getItemsByIdStr(institutions.getMatters());
        institutions.setMatters(StringUtils.join(items, ","));
        List<InstitutionPersonEntity> institutionPersonList = institutionPersonService.list(new QueryWrapper<InstitutionPersonEntity>().eq("institutions_id", id));
        institutions.setInstitutionPerson(institutionPersonList);
        return R.ok().put("institutions", institutions);
    }

    /**
     * 信息
     */
    @GetMapping("/aInfo")
    @ApiOperation(value = "信息")
    public R aInfo(@RequestParam("id") Integer id) {
        InstitutionsEntity institutions = institutionsService.getById(id);
        List<String> items = mattersService.getItemsByIdStr(institutions.getMatters());
        institutions.setMatters(StringUtils.join(items, ","));
        List<InstitutionPersonEntity> institutionPersonList = institutionPersonService.list(new QueryWrapper<InstitutionPersonEntity>().eq("institutions_id", id));
        institutions.setInstitutionPerson(institutionPersonList);
        return R.ok().put("institutions", institutions);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody InstitutionsEntity institutions) {
        institutionsService.save(institutions);
        List<InstitutionPersonEntity> institutionPerson = institutions.getInstitutionPerson();
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody InstitutionsEntity institutions) {
        institutionsService.updateById(institutions);
        institutionPersonService.remove(new QueryWrapper<InstitutionPersonEntity>().eq("institutions_id", institutions.getId()));
        if (institutions.getInstitutionPerson() != null && institutions.getInstitutionPerson().size() > 0) {
            List<InstitutionPersonEntity> institutionPerson = institutions.getInstitutionPerson();
            institutionPerson.forEach(e -> {
                e.setInstitutionsId(institutions.getId());
            });
            institutionPersonService.saveBatch(institutionPerson);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @RequiresPermissions("generator:institutions:delete")
    public R delete(@RequestBody Integer[] ids) {
        institutionsService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @PostMapping("/updateList")
    public R updateList(@RequestBody Map<String, Object> map) {
        List<Map> list = (List<Map>) map.get("list");
        list.forEach(i -> {
            InstitutionsEntity one = institutionsService.getOne(new QueryWrapper<InstitutionsEntity>().eq("name", i.get("name")));
            if (one != null) {
                one.setCreditCode((String) i.get("daima"));
                one.setRegistrationTime(DateUtil.parse((String) i.get("createDate")));
                one.setRegisteredCapital((String) i.get("ziben"));
                one.setRegisteredAddress((String) i.get("address"));
                one.setScopeBusiness((String) i.get("jingying"));
                one.setOfficeAddress((String) i.get("bangongaddress"));
                one.setLegalPerson((String) i.get("faren"));
                one.setContact((String) i.get("lianxiren"));
                one.setContactPhone((String) i.get("lianxirenphone"));
                one.setUnitIntroduction((String) i.get("danweijianjie"));
                institutionsService.updateById(one);
            }

        });
        return R.ok();

    }

}
